package cn.golxen.blog.service.impl;

import cn.golxen.blog.commons.utils.JWTUtils;
import cn.golxen.blog.commons.vo.CheckAdminVo;
import cn.golxen.blog.commons.vo.ResCheckAdminVo;
import cn.golxen.blog.exception.exceptionClass.GetTokenException;
import cn.golxen.blog.mapper.AdminMapper;
import cn.golxen.blog.redis.AdminRedis;
import cn.golxen.blog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminRedis rAdmin;
    @Autowired
    @Qualifier(value = "taskExecutor")
    ThreadPoolTaskExecutor poolTaskExecutor;

    @Override
    public Integer updateAvatar(String url) {
        Integer integer=adminMapper.updateAvatar(url);
        poolTaskExecutor.execute(()->{
            rAdmin.deleteByRedisKey("avatar");
        });
        return integer;
    }

    @Override
    public Integer updateSignature(String signature) {
        Integer integer=adminMapper.updateSignature(signature);
        poolTaskExecutor.execute(()->{
            rAdmin.deleteByRedisKey("signature");
        });
        return integer;
    }

    @Override
    public Integer updateResume(String resume) {
        Integer integer=adminMapper.updateResume(resume);
        poolTaskExecutor.execute(()->{
            rAdmin.deleteByRedisKey("resume");
        });
        return integer;
    }

    @Override
    public String getAvatar() {
        return rAdmin.getAvatar();
    }

    @Override
    public String getSignature() {
        return rAdmin.getSignature();
    }

    @Override
    public String getResume() {
        return rAdmin.getResume();
    }

    @Override
    public ResCheckAdminVo checkAdmin(CheckAdminVo checkAdminVo) {
        Integer integer=adminMapper.checkAdmin(checkAdminVo);
        if (integer!=0){
            String token= JWTUtils.createJwtToken(checkAdminVo.getId());
            return new ResCheckAdminVo(token, checkAdminVo.isRemember());
        }else
            throw new GetTokenException();
    }

    @Override
    public void flushDB() {
        rAdmin.flushDB();
    }
}
